package com.vencent.spring.aop;

import java.util.List;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;

import com.vencent.spring.util.ResultMapUtils;



/**
 * 
 * @Description 对控制器中字段的判断
 * @Date 2017年3月3日 上午10:13:09
 * @author wbw
 * @version 1.0.0
 */
@Component
@Aspect
public class ControllerReturnAop {
	/**
	 * 拦截返回
	 * @param join
	 * @param o
	 * @param errors
	 * @return
	 */
	@Around("@annotation(org.springframework.web.bind.annotation.RequestMapping) && args(..,errors)")
	public Object returningParam(ProceedingJoinPoint join, Errors errors){
		try {
			if(errors.hasErrors()){
				List<ObjectError> list = errors.getAllErrors();
				for(ObjectError obj : list){
					String errorStr = obj.getDefaultMessage();
					return ResultMapUtils.getResultMap(null, "-200", errorStr);
				}
			}
			return join.proceed();
		} catch (Throwable e) {
			return null;
		}
		
	}
}
